﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class FinanceCenter_Earning4 : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        if (!this.IsPostBack)
        {
            if (DateTime.Today == DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).AddDays(-1))            
            {
                this.btnAllDistlll.Enabled = true;
            }
            
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;        
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        RequestCompetences = Competences.BuildCompetencesList(Competences.Administrator);

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = null;
        DataSet ds = null;

        DateTime Date = DateTime.Today;
            
        if(tbDate.Value != null && tbDate.Value != "")
        {
            Date = Shove._Convert.StrToDateTime(tbDate.Value, "");
        }

        DAL.Procedures.P_Earning4(ref ds, Date);        

        if (ds.Tables.Count == 0 || ds == null)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无您需要的数据", "Earning4");

            return;
        }

        dt = ds.Tables[0];

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            bool IsDistill = Shove._Convert.StrToBool(dr["IsDistill"].ToString(), false);
            Button btnDistill = (Button)e.Item.Cells[6].FindControl("btnDistill");

            if (IsDistill)
            {
                e.Item.Cells[5].Text = "已结";                
            }
            else
            {
                e.Item.Cells[5].Text = "未结";
                
                if (DateTime.Today == DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).AddDays(-1))// 是否是本月最后一天                
                {
                    btnDistill.Enabled = true;
                }
            }
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnSerchAll_Click(object sender, EventArgs e)
    {
        gPager.PageIndex = 0;

        BindData();
    }

    protected void g_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        long Earning4ID = Shove._Convert.StrToLong(e.Item.Cells[7].Text, -1);

        string ReturnDescription = "";

        if (e.CommandName == "Distill")
        {
            if (DAL.Procedures.P_DistillEarning4(1, Earning4ID, ref ReturnDescription) < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, ReturnDescription);

                return;
            }

            BindData();

            return;
        }

    }

    protected void btnAllDistlll_Click(object sender, EventArgs e)
    {
        string ReturnDescription = "";

        if (DAL.Procedures.P_DistillEarning4(2, 0, ref ReturnDescription) < 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, ReturnDescription);

            return;
        }

        BindData();
    }

    protected void btnGO_Click(object sender, System.EventArgs e)
    {
        BindData();
    }

}